from datetime import datetime

from werkzeug.security import generate_password_hash, check_password_hash
from app import db, constants

class User(db.Model):
    """入库用户"""

    __tablename__ = "tb_user_profile"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 用户编号
    name = db.Column(db.String(32), unique=True, nullable=False)  # 用户昵称
    password_hash = db.Column(db.String(256), nullable=False)  # 加密的密码
    mobile = db.Column(db.String(11), unique=True, nullable=False)  # 手机号
    mail = db.Column(db.String(255))
    power = db.Column(db.String(11), default='user')
    login_time = db.Column(db.DateTime, default=datetime.now)  # 记录的最近一次登录时间
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(db.DateTime, onupdate=datetime.now)  # 记录的更新时间



    # 通过装饰器property，把password方法提升为属性
    @property
    def password(self):
        """获取password属性时被调用"""
        raise AttributeError("不可读")

    @password.setter
    def password(self, passwd):
        """设置password属性时被调用，设置密码加密"""
        self.password_hash = generate_password_hash(passwd)

    def copy_passwordHash(self, passwdHash):
        """检查密码的正确性"""
        self.password_hash = passwdHash

    def check_password(self, passwd):
        """检查密码的正确性"""
        return check_password_hash(self.password_hash, passwd)

    def to_dict(self):
        """将对象转换为字典数据"""
        user_dict = {
            "user_id": self.id,
            "name": self.name,
            "mobile": self.mobile,
            "power": self.power,
            "mail": self.mail,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "login_time": self.login_time.strftime("%Y-%m-%d %H:%M:%S")
        }
        return user_dict

class UserApply(db.Model):
    """用户注册申请处理表"""

    __tablename__ = "tb_user_apply"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 用户编号
    name = db.Column(db.String(32), unique=True, nullable=False)  # 用户昵称
    password_hash = db.Column(db.String(256), nullable=False)  # 加密的密码
    mobile = db.Column(db.String(11), unique=True, nullable=False)  # 手机号
    mail = db.Column(db.String(255))
    power = db.Column(db.String(11), default='user')
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    handleResult = db.Column(db.String(32), default='NotProcessedYet')  # 当前处理结果（NotProcessedYet， Rejected， Agreed）



    # 通过装饰器property，把password方法提升为属性
    @property
    def password(self):
        """获取password属性时被调用"""
        raise AttributeError("不可读")

    @password.setter
    def password(self, passwd):
        """设置password属性时被调用，设置密码加密"""
        self.password_hash = generate_password_hash(passwd)

    def check_password(self, passwd):
        """检查密码的正确性"""
        return check_password_hash(self.password_hash, passwd)

    def to_dict(self):
        """将对象转换为字典数据"""
        user_dict = {
            "user_id": self.id,
            "name": self.name,
            "mobile": self.mobile,
            "mail": self.mail,
            "power": self.power,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
        }
        return user_dict

class GeologyWP(db.Model):


    __tablename__ = "tb_geology_workpoint"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    county = db.Column(db.String(50), nullable=False)
    town = db.Column(db.String(50))
    latitude = db.Column(db.String(50))
    longitude = db.Column(db.String(50))
    id_sample = db.Column(db.String(50), nullable=False)
    time_batch = db.Column(db.Integer)
    time = db.Column(db.String(50))

class SoilWP(db.Model):


    __tablename__ = "tb_soil_workpoint"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    county = db.Column(db.String(50), nullable=False)
    town = db.Column(db.String(50))
    latitude = db.Column(db.String(50))
    longitude = db.Column(db.String(50))
    id_sample = db.Column(db.String(50), nullable=False)
    time_batch = db.Column(db.Integer)
    soil_type = db.Column(db.String(50))
    number_altitude_zone = db.Column(db.Float)
    number_soil_sample = db.Column(db.Float)
    time = db.Column(db.String(50))

class GeologyPro(db.Model):
    __tablename__ = "tb_geology_whole_pro"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    id_sample = db.Column(db.String(50), nullable=False)
    Class = db.Column(db.String(100))
    number = db.Column(db.Float)
    slide_number = db.Column(db.Float)
    slide_volume = db.Column(db.Float)
    slide_description = db.Column(db.String(200))
    mudslide_number = db.Column(db.Float)
    mudslide_volume = db.Column(db.String(255))
    collapse_number = db.Column(db.Float)
    collapse_volume = db.Column(db.Float)
    collapse_description = db.Column(db.String(200))

class SoilPro(db.Model):
    __tablename__ = "tb_soil_whole_pro"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    county = db.Column(db.String(50))
    bulk = db.Column(db.Float)
    humidity = db.Column(db.Float)
    ph = db.Column(db.Float)
    som = db.Column(db.Float)
    tn = db.Column(db.Float)
    tp = db.Column(db.Float)
    tk = db.Column(db.Float)
    tc = db.Column(db.Float)
    available_p = db.Column(db.Float)
    available_n = db.Column(db.Float)
    available_k = db.Column(db.Float)
    h = db.Column(db.Float)
    s = db.Column(db.Float)
    time_batch = db.Column(db.Integer)
    time = db.Column(db.String(50))

class SoilStatistics(db.Model):
    __tablename__ = "tb_soil_spatial_statistics"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    county = db.Column(db.String(50))
    c = db.Column(db.Float)
    d = db.Column(db.Float)
    m = db.Column(db.Float)
    n = db.Column(db.Float)
    q = db.Column(db.Float)
    u = db.Column(db.Float)
    f = db.Column(db.Float)
    o = db.Column(db.Float)
    e = db.Column(db.Float)
    k = db.Column(db.Float)



class zoologyWP(db.Model):

    __tablename__ = "tb_zoology_vegetation_workpoint"

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    time_batch = db.Column(db.Integer)
    number_plot = db.Column(db.Integer)
    number_altitude_zone = db.Column(db.Integer)
    county = db.Column(db.String(50), nullable=False)
    town = db.Column(db.String(50))
    latitude = db.Column(db.String(50))
    longitude = db.Column(db.String(50))
    id_sample = db.Column(db.String(50), nullable=False)
    id_repair = db.Column(db.String(50))
    name_community = db.Column(db.String(50))
    number_species = db.Column(db.Integer)
    dominate_species = db.Column(db.String(50))
    time = db.Column(db.String(50))
    fcover = db.Column(db.Float)
    fresh_biomass_community = db.Column(db.Float)
    above_biomass = db.Column(db.Float)
    fresh_under_biomass = db.Column(db.Float)
    under_biomass = db.Column(db.Float)
    name_repair = db.Column(db.String(255))
